package top.heue.test.fragment.mvi

import top.heue.utils.AD
import top.heue.utils.lifecycle.base.BaseMVIModel
import top.heue.utils.lifecycle.base.IUiEffect
import top.heue.utils.lifecycle.base.IUiIntent
import top.heue.utils.lifecycle.base.IUiState
import top.heue.utils.log.L

data class TestUIState(val state1: State1, val state2: State2) : IUiState

sealed class State1 {
    object Init : State1()
    data class Loading(var isShow: Boolean) : State1()
}

sealed class State2 {
    object Init : State2()
    data class Success(var isSuccess: Boolean) : State2()
}

sealed class TestUIEffect : IUiEffect {
    object ShowDialogState : TestUIEffect()
}

sealed class TestIntent : IUiIntent {
    object Touch : TestIntent()
    object ShowDialog : TestIntent()
}

class TestMVIModel : BaseMVIModel<TestIntent, TestUIState, TestUIEffect>() {
    override val initUiState: TestUIState = TestUIState(State1.Init, State2.Init)
    override fun onIntent(intent: TestIntent) {
        when (intent) {
            is TestIntent.Touch -> {
                sendUiState {
                    if (state1 is State1.Loading) {
                        copy(state2 = State2.Success(false))
                    } else copy(state1 = State1.Loading(true))
                }
            }

            is TestIntent.ShowDialog -> {
                sendUiEffect { TestUIEffect.ShowDialogState }
                L.e(currentUiState)
            }
        }
    }
}